import { CacheService } from './CacheService';
import { ModelService } from './ModelService';
import { ValidationService } from './ValidationService';
import { ImportExportService } from './ImportExportService';

/**
 * 服务管理器 - 单例模式
 * 统一管理所有服务实例，确保服务之间的依赖关系正确
 */
export class ServiceManager {
  private static instance: ServiceManager;
  
  private _cacheService: CacheService;
  private _modelService: ModelService;
  private _validationService: ValidationService;
  private _importExportService: ImportExportService;

  private constructor() {
    // 初始化服务实例，注意依赖关系
    this._cacheService = new CacheService();
    this._modelService = new ModelService(this._cacheService);
    this._validationService = new ValidationService(this._modelService, this._cacheService);
    this._importExportService = new ImportExportService();
  }

  /**
   * 获取服务管理器实例
   */
  public static getInstance(): ServiceManager {
    if (!ServiceManager.instance) {
      ServiceManager.instance = new ServiceManager();
    }
    return ServiceManager.instance;
  }

  /**
   * 获取缓存服务
   */
  public get cacheService(): CacheService {
    return this._cacheService;
  }

  /**
   * 获取模型服务
   */
  public get modelService(): ModelService {
    return this._modelService;
  }

  /**
   * 获取验证服务
   */
  public get validationService(): ValidationService {
    return this._validationService;
  }

  /**
   * 获取导入导出服务
   */
  public get importExportService(): ImportExportService {
    return this._importExportService;
  }

  /**
   * 初始化所有服务（可选的预热操作）
   */
  public async initialize(): Promise<void> {
    try {
      // 清理过期缓存
      await this._cacheService.clearExpired();
      
      console.log('ServiceManager 初始化完成');
    } catch (error) {
      console.error('ServiceManager 初始化失败:', error);
    }
  }

  /**
   * 销毁服务管理器（清理资源）
   */
  public async destroy(): Promise<void> {
    try {
      // 这里可以添加清理逻辑
      console.log('ServiceManager 已销毁');
    } catch (error) {
      console.error('ServiceManager 销毁失败:', error);
    }
  }
}